package com.csproj.dispatch.resource;

import com.codahale.metrics.annotation.Timed;
import com.csproj.dispatch.service.StatisticsService;
import com.csproj.dispatch.util.entity.*;
import com.csproj.siiat.base.common.dto.BaseDTO;
import com.csproj.siiat.config.Languagesproperties;
import com.csproj.system.domain.Language;
import com.csproj.system.domain.UserInfo;
import com.csproj.system.repository.LanguageRepository;
import com.csproj.system.service.SysLogsService;
import com.csproj.system.service.UserInfoService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 统计
 */
@RestController
@RequestMapping("/api/center/statistics")
public class StatisticsResource {
    private final Logger log = LoggerFactory.getLogger(StatisticsResource.class);

    private static final String ENTITY_NAME = "statistics";

    private final StatisticsService statisticsService;
    private final UserInfoService userInfoService;
    private final SysLogsService sysLogsService;
    private final Languagesproperties languagesproperties;
    private final LanguageRepository languageRepository;
    private String userTimes;

    public StatisticsResource(StatisticsService statisticsService, UserInfoService userInfoService, SysLogsService sysLogsService, Languagesproperties languagesproperties, LanguageRepository languageRepository) {
        this.statisticsService = statisticsService;
        this.userInfoService = userInfoService;
        this.sysLogsService = sysLogsService;
        this.languagesproperties = languagesproperties;
        this.languageRepository = languageRepository;
    }


    /**
     * 路径使用频率统计
     *
     * @param limit             查询数 【必须】
     * @param startTime         起始时间 yyyy-MM-dd
     * @param endTime           结束时间 yyyy-MM-dd
     * @param sort              排序规则 desc/asc 【必须】
     * @param launchsiteId      起始站点id
     * @param destinationsiteId 目的站点id
     * @return
     */
    @GetMapping("/route/usefrequency")
    @Timed
    public BaseDTO usefrequency(Integer limit, @RequestParam(value = "startTime", required = false) ZonedDateTime startTime,
                                @RequestParam(value = "endTime", required = false) ZonedDateTime endTime,
                                @RequestParam(value = "sort") String sort,
                                @RequestParam(value = "launchsiteId", required = false) String launchsiteId,
                                @RequestParam(value = "destinationsiteId", required = false) String destinationsiteId) {
        UserInfo userInfo = userInfoService.getCurrentUser();
        sysLogsService.saveForGet("", userInfo.getName() + ":user_use_frequency_path", "");
        return statisticsService.usefrequency(limit, startTime, endTime, sort, launchsiteId, destinationsiteId);
    }

    /**
     * 路径使用频率统计
     *
     * @param startTime         起始时间 yyyy-MM-dd
     * @param endTime           结束时间 yyyy-MM-dd
     * @param sort              排序规则 desc/asc 【必须】
     * @param launchsiteId      起始站点id
     * @param destinationsiteId 目的站点id
     * @return
     */
    @GetMapping("/route/usefrequencyexport")
    @Timed
    public void usefrequencyExport(HttpServletResponse response, @RequestParam(value = "startTime", required = false) ZonedDateTime startTime,
                                      @RequestParam(value = "endTime", required = false) ZonedDateTime endTime,
                                      @RequestParam(value = "sort") String sort,
                                      @RequestParam(value = "launchsiteId", required = false) String launchsiteId,
                                      @RequestParam(value = "destinationsiteId", required = false) String destinationsiteId) {
        String pathNo = "";
        String startSite = "";
        String endSite = "";
        String userTimes = "";
        Map<String, Object> currentMp = new HashMap<String, Object>() {
        };
        List<Language> l = languageRepository.findByDefault();
        Language first = null;
        if (l.size() > 0) {
            first = l.get(0);
        }

        if (first != null) {
            if (first.getName().equals("zh")) {
                currentMp = languagesproperties.getZh();
            } else if (first.getName().equals("en")) {
                currentMp = languagesproperties.getEn();

            } else if (first.getName().equals("pu")) {
                currentMp = languagesproperties.getPu();
            }

            if (currentMp != null) {
                pathNo = currentMp.get("path_no").toString();
                startSite = currentMp.get("start_site").toString();
                endSite = currentMp.get("end_site").toString();
                userTimes = currentMp.get("use_times").toString();

            }


        }


        String[] headers = {pathNo, startSite, endSite, userTimes};

        List<RouteFrequency> list = statisticsService.usefrequencyExport(startTime, endTime, sort,launchsiteId, destinationsiteId);

        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        //设置列宽
        sheet.setDefaultColumnWidth((short) 18);
        //创建第一行的对象，第一行一般用于填充标题内容。从第二行开始一般是数据
        HSSFRow row = sheet.createRow(0);
        for (
                short i = 0;
                i < headers.length; i++) {
            //创建单元格，每行多少数据就创建多少个单元格
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            //给单元格设置内容
            cell.setCellValue(text);
        }

        //遍历集合，将每个集合元素对象的每个值填充到单元格中
        for (
                int i = 0; i < list.size(); i++) {
            RouteFrequency item = list.get(i);
            //从第二行开始填充数据
            row = sheet.createRow(i + 1);

            HSSFCell cell = row.createCell(0);
            cell.setCellValue(item.getRouteCode());
            HSSFCell cell1 = row.createCell(1);
            cell1.setCellValue(item.getLaunchsite());
            HSSFCell cell2 = row.createCell(2);
            cell2.setCellValue(item.getDestinationsite());
            HSSFCell cell3 = row.createCell(3);
            cell3.setCellValue(item.getFrequency());

        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + "export.xls");//Excel文件名
        try {
            response.flushBuffer();
        } catch (
                IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //将workbook中的内容写入输出流中
        try {
            workbook.write(response.getOutputStream());
        } catch (
                IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 路径安全趋势统计
     *
     * @param limit             查询数 【必须】
     * @param startTime         起始时间 yyyy-MM-dd
     * @param endTime           结束时间 yyyy-MM-dd
     * @param sort              排序规则 desc/asc 【必须】
     * @param launchsiteId      起始站点id
     * @param destinationsiteId 目的站点id
     * @return
     */
    @GetMapping("/route/securitytrend")
    @Timed
    public BaseDTO securityTrend(Integer limit, @RequestParam(value = "startTime", required = false) ZonedDateTime startTime,
                                 @RequestParam(value = "endTime", required = false) ZonedDateTime endTime,
                                 @RequestParam(value = "sort") String sort,
                                 @RequestParam(value = "launchsiteId", required = false) String launchsiteId,
                                 @RequestParam(value = "destinationsiteId", required = false) String destinationsiteId) {

        UserInfo userInfo = userInfoService.getCurrentUser();
        sysLogsService.saveForGet("", userInfo.getName() + ":user_use_safe_frequency_path", "");
        return statisticsService.securityTrend(limit, startTime, endTime, sort, launchsiteId, destinationsiteId);
    }

    /**
     * 路径安全趋势统计
     *
     * @param limit             查询数 【必须】
     * @param startTime         起始时间 yyyy-MM-dd
     * @param endTime           结束时间 yyyy-MM-dd
     * @param sort              排序规则 desc/asc 【必须】
     * @param launchsiteId      起始站点id
     * @param destinationsiteId 目的站点id
     * @return
     */
    @GetMapping("/route/securitytrendexport")
    @Timed
    public void securityTrendExport(HttpServletResponse response,Integer limit, @RequestParam(value = "startTime", required = false) ZonedDateTime startTime,
                                       @RequestParam(value = "endTime", required = false) ZonedDateTime endTime,
                                       @RequestParam(value = "sort") String sort,
                                       @RequestParam(value = "launchsiteId", required = false) String launchsiteId,
                                       @RequestParam(value = "destinationsiteId", required = false) String destinationsiteId) {
        String pathNo = "";
        String startSite = "";
        String endSite = "";
        String userTimes = "";
        Map<String, Object> currentMp = new HashMap<String, Object>() {
        };
        List<Language> l = languageRepository.findByDefault();
        Language first = null;
        if (l.size() > 0) {
            first = l.get(0);
        }

        if (first != null) {
            if (first.getName().equals("zh")) {
                currentMp = languagesproperties.getZh();
            } else if (first.getName().equals("en")) {
                currentMp = languagesproperties.getEn();

            } else if (first.getName().equals("pu")) {
                currentMp = languagesproperties.getPu();
            }

            if (currentMp != null) {
                pathNo = currentMp.get("path_no").toString();
                startSite = currentMp.get("start_site").toString();
                endSite = currentMp.get("end_site").toString();
                userTimes = currentMp.get("warn_time").toString();

            }


        }

        String[] headers = {pathNo, startSite, endSite, userTimes};
        List<RouteFrequency> list = statisticsService.securityTrendExport(limit, startTime, endTime, sort, launchsiteId, destinationsiteId);
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        //设置列宽
        sheet.setDefaultColumnWidth((short) 18);
        //创建第一行的对象，第一行一般用于填充标题内容。从第二行开始一般是数据
        HSSFRow row = sheet.createRow(0);
        for (
                short i = 0;
                i < headers.length; i++) {
            //创建单元格，每行多少数据就创建多少个单元格
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            //给单元格设置内容
            cell.setCellValue(text);
        }

        //遍历集合，将每个集合元素对象的每个值填充到单元格中
        for (
                int i = 0; i < list.size(); i++) {
            RouteFrequency item = list.get(i);
            //从第二行开始填充数据
            row = sheet.createRow(i + 1);

            HSSFCell cell = row.createCell(0);
            cell.setCellValue(item.getRouteCode());
            HSSFCell cell1 = row.createCell(1);
            cell1.setCellValue(item.getLaunchsite());
            HSSFCell cell2 = row.createCell(2);
            cell2.setCellValue(item.getDestinationsite());
            HSSFCell cell3 = row.createCell(3);
            cell3.setCellValue(item.getFrequency());

        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + "export.xls");//Excel文件名
        try {
            response.flushBuffer();
        } catch (
                IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //将workbook中的内容写入输出流中
        try {
            workbook.write(response.getOutputStream());
        } catch (
                IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 路径报警趋势统计
     *
     * @param startTime         起始时间 yyyy-MM-dd
     * @param endTime           结束时间 yyyy-MM-dd
     * @param launchsiteId      起始站点id 【必须】
     * @param destinationsiteId 目的站点id
     * @param routeId           路径id
     * @return
     */
    @GetMapping("/route/warntrend")
    @Timed
    public BaseDTO warnTrend(@RequestParam(value = "startTime", required = false) ZonedDateTime startTime,
                             @RequestParam(value = "endTime", required = false) ZonedDateTime endTime,
                             @RequestParam(value = "launchsiteId") String launchsiteId,
                             @RequestParam(value = "destinationsiteId", required = false) String destinationsiteId,
                             @RequestParam(value = "routeId", required = false) String routeId) {
        UserInfo userInfo = userInfoService.getCurrentUser();
        sysLogsService.saveForGet("", userInfo.getName() + ":user_use_warn_frequency_path", "");

        return statisticsService.warnTrend(startTime, endTime, launchsiteId, destinationsiteId, routeId);
    }

    /**
     * 路径报警趋势统计
     *
     * @param startTime         起始时间 yyyy-MM-dd
     * @param endTime           结束时间 yyyy-MM-dd
     * @param launchsiteId      起始站点id 【必须】
     * @param destinationsiteId 目的站点id
     * @param routeId           路径id
     * @return
     */
    @GetMapping("/route/warntrendexport")
    @Timed
    public void warnTrendExport(HttpServletResponse response, @RequestParam(value = "startTime", required = false) ZonedDateTime startTime,
                                   @RequestParam(value = "endTime", required = false) ZonedDateTime endTime,
                                   @RequestParam(value = "launchsiteId") String launchsiteId,
                                   @RequestParam(value = "destinationsiteId", required = false) String destinationsiteId,
                                   @RequestParam(value = "routeId", required = false) String routeId) {
        String warn_type = "";
        String warn_time = "";
        Map<String, Object> currentMp = new HashMap<String, Object>() {
        };
        List<Language> l = languageRepository.findByDefault();
        Language first = null;
        if (l.size() > 0) {
            first = l.get(0);
        }

        if (first != null) {
            if (first.getName().equals("zh")) {
                currentMp = languagesproperties.getZh();
            } else if (first.getName().equals("en")) {
                currentMp = languagesproperties.getEn();

            } else if (first.getName().equals("pu")) {
                currentMp = languagesproperties.getPu();
            }

            if (currentMp != null) {
                warn_type = currentMp.get("warn_type").toString();
                warn_time = currentMp.get("warn_time").toString();

            }


        }

        String[] headers = {warn_type, warn_time};

        List<RouteTrend> list = statisticsService.warnTrendExport(startTime, endTime, launchsiteId, destinationsiteId, routeId);

        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        //设置列宽
        sheet.setDefaultColumnWidth((short) 18);
        //创建第一行的对象，第一行一般用于填充标题内容。从第二行开始一般是数据
        HSSFRow row = sheet.createRow(0);
        for (
                short i = 0;
                i < headers.length; i++) {
            //创建单元格，每行多少数据就创建多少个单元格
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            //给单元格设置内容
            cell.setCellValue(text);
        }

        //遍历集合，将每个集合元素对象的每个值填充到单元格中
        for (
                int i = 0; i < list.size(); i++) {
            RouteTrend item = list.get(i);
            //从第二行开始填充数据
            row = sheet.createRow(i + 1);

            HSSFCell cell = row.createCell(0);
            cell.setCellValue(item.getWarnType());
            HSSFCell cell1 = row.createCell(1);
            cell1.setCellValue(item.getFrequency());

        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + "export.xls");//Excel文件名
        try {
            response.flushBuffer();
        } catch (
                IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //将workbook中的内容写入输出流中
        try {
            workbook.write(response.getOutputStream());
        } catch (
                IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 关锁使用统计
     *
     * @param limit     查询数 【必须】
     * @param startTime 起始时间 yyyy-MM-dd
     * @param endTime   结束时间 yyyy-MM-dd
     * @param sort      排序规则 desc/asc 【必须】
     * @return
     */
    @GetMapping("/route/lockusecount")
    @Timed
    public BaseDTO lockUseCont(Integer limit,
                               @RequestParam(value = "siteId", required = false) String siteId,
                               @RequestParam(value = "startTime", required = false) ZonedDateTime startTime,
                               @RequestParam(value = "endTime", required = false) ZonedDateTime endTime,
                               String sort) {
        UserInfo userInfo = userInfoService.getCurrentUser();
        sysLogsService.saveForGet("", userInfo.getName() + ":user_use_warn_frequency_lock", "");
        return statisticsService.lockUseCont(limit, siteId, startTime, endTime, sort);
    }

    /**
     * 关锁使用统计
     *
     * @param limit     查询数 【必须】
     * @param startTime 起始时间 yyyy-MM-dd
     * @param endTime   结束时间 yyyy-MM-dd
     * @param sort      排序规则 desc/asc 【必须】
     * @return
     */
    @GetMapping("/route/lockuseexport")
    @Timed
    public void lockUseExport(HttpServletResponse response,Integer limit,
                                 @RequestParam(value = "siteId", required = false) String siteId,
                                 @RequestParam(value = "startTime", required = false) ZonedDateTime startTime,
                                 @RequestParam(value = "endTime", required = false) ZonedDateTime endTime,
                                 String sort) {
        String pathNo = "";
        String startSite = "";
        String endSite = "";
        String userTimes = "";
        Map<String, Object> currentMp = new HashMap<String, Object>() {
        };
        List<Language> l = languageRepository.findByDefault();
        Language first = null;
        if (l.size() > 0) {
            first = l.get(0);
        }

        if (first != null) {
            if (first.getName().equals("zh")) {
                currentMp = languagesproperties.getZh();
            } else if (first.getName().equals("en")) {
                currentMp = languagesproperties.getEn();

            } else if (first.getName().equals("pu")) {
                currentMp = languagesproperties.getPu();
            }

            if (currentMp != null) {
                pathNo = currentMp.get("path_no").toString();
                startSite = currentMp.get("site_name").toString();
//                endSite = currentMp.get("end_site").toString();
                userTimes = currentMp.get("use__frequency_lock_number").toString();

            }


        }

        String[] headers = {pathNo, startSite, userTimes};
        List<LockUseCount> list = statisticsService.lockUseContExport(limit, siteId, startTime, endTime, sort);
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        //设置列宽
        sheet.setDefaultColumnWidth((short) 18);
        //创建第一行的对象，第一行一般用于填充标题内容。从第二行开始一般是数据
        HSSFRow row = sheet.createRow(0);
        for (
                short i = 0;
                i < headers.length; i++) {
            //创建单元格，每行多少数据就创建多少个单元格
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            //给单元格设置内容
            cell.setCellValue(text);
        }

        //遍历集合，将每个集合元素对象的每个值填充到单元格中
        for (
                int i = 0; i < list.size(); i++) {
            LockUseCount item = list.get(i);
            //从第二行开始填充数据
            row = sheet.createRow(i + 1);

            HSSFCell cell = row.createCell(0);
            cell.setCellValue(item.getLockCode());
            HSSFCell cell2 = row.createCell(1);
            cell2.setCellValue(item.getSiteName());
            HSSFCell cell3 = row.createCell(2);
            cell3.setCellValue(item.getFrequency());

        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + "export.xls");//Excel文件名
        try {
            response.flushBuffer();
        } catch (
                IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //将workbook中的内容写入输出流中
        try {
            workbook.write(response.getOutputStream());
        } catch (
                IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 路径使用时间统计--秒
     *
     * @param limit     查询数 【必须】
     * @param startTime 起始时间 yyyy-MM-dd
     * @param endTime   结束时间 yyyy-MM-dd
     * @param sort      排序规则 desc/asc 【必须】
     * @return
     */
    @GetMapping("/route/arrivetimecount")
    @Timed
    public BaseDTO arriveTimeCount(Integer limit, @RequestParam(value = "startTime", required = false) ZonedDateTime startTime,
                                   @RequestParam(value = "endTime", required = false) ZonedDateTime endTime, String sort) {

        UserInfo userInfo = userInfoService.getCurrentUser();
        sysLogsService.saveForGet("", userInfo.getName() + ":user_use__frequency_lock", "");
        return statisticsService.arriveTimeCount(limit, startTime, endTime, sort);
    }

    /**
     * 路径使用时间统计--秒
     *
     * @param limit     查询数 【必须】
     * @param startTime 起始时间 yyyy-MM-dd
     * @param endTime   结束时间 yyyy-MM-dd
     * @param sort      排序规则 desc/asc 【必须】
     * @return
     */
    @RequestMapping("/route/exportPathUse")
    @Timed
    public void exportPathUse(HttpServletRequest request, HttpServletResponse response, Integer limit, @RequestParam(value = "startTime", required = false) ZonedDateTime startTime,
                              @RequestParam(value = "endTime", required = false) ZonedDateTime endTime, String sort) {

        String pathNo = "";
        String startSite = "";
        String endSite = "";
        String userTimes = "";
        Map<String, Object> currentMp = new HashMap<String, Object>() {
        };
        List<Language> l = languageRepository.findByDefault();
        Language first = null;
        if (l.size() > 0) {
            first = l.get(0);
        }

        if (first != null) {
            if (first.getName().equals("zh")) {
                currentMp = languagesproperties.getZh();
            } else if (first.getName().equals("en")) {
                currentMp = languagesproperties.getEn();

            } else if (first.getName().equals("pu")) {
                currentMp = languagesproperties.getPu();
            }

            if (currentMp != null) {
                pathNo = currentMp.get("path_no").toString();
                startSite = currentMp.get("start_site").toString();
                endSite = currentMp.get("end_site").toString();
                userTimes = currentMp.get("use__frequency_lock_number").toString();
            }
        }


        String[] headers = {pathNo, startSite, endSite, userTimes};

        List<ArriveTimeCount> list = statisticsService.arriveTimeCountForExport(startTime, endTime, sort);

        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        //设置列宽
        sheet.setDefaultColumnWidth((short) 18);
        //创建第一行的对象，第一行一般用于填充标题内容。从第二行开始一般是数据
        HSSFRow row = sheet.createRow(0);
        for (
                short i = 0;
                i < headers.length; i++) {
            //创建单元格，每行多少数据就创建多少个单元格
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            //给单元格设置内容
            cell.setCellValue(text);
        }

        //遍历集合，将每个集合元素对象的每个值填充到单元格中
        for (
                int i = 0; i < list.size(); i++) {
            ArriveTimeCount item = list.get(i);
            //从第二行开始填充数据
            row = sheet.createRow(i + 1);

            HSSFCell cell = row.createCell(0);
            cell.setCellValue(item.getRouteCode());
            HSSFCell cell1 = row.createCell(1);
            cell1.setCellValue(item.getLaunchsite());
            HSSFCell cell2 = row.createCell(2);
            cell2.setCellValue(item.getDestinationsite());
            HSSFCell cell3 = row.createCell(3);
            cell3.setCellValue(item.getTimeStr());

        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + "Devices.xls");//Excel文件名
        try {
            response.flushBuffer();
        } catch (
                IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //将workbook中的内容写入输出流中
        try {
            workbook.write(response.getOutputStream());
        } catch (
                IOException e) {
            e.printStackTrace();
        }

    }


    /**
     * 关锁业务量统计
     *
     * @param limit     limit 查询数 【必须】
     * @param startTime startTime 起始时间 yyyy-MM-dd
     * @param endTime   endTime 结束时间 yyyy-MM-dd
     * @param sort      sort 排序规则 desc/asc 【必须】
     * @param siteId    站点id
     * @param busType   查询类型 1：转运业务量 2：抵运业务量 0/其他：全部业务 【必须】
     * @return
     */
    @GetMapping("/route/lockbusinesscount")
    @Timed
    public BaseDTO lockBusinessCount(Integer limit, @RequestParam(value = "startTime", required = false) ZonedDateTime startTime,
                                     @RequestParam(value = "endTime", required = false) ZonedDateTime endTime, String sort,
                                     String siteId, Integer busType) {

        UserInfo userInfo = userInfoService.getCurrentUser();
        sysLogsService.saveForGet("", userInfo.getName() + ":user_use_frequency_lock", "");
        return statisticsService.lockBusinessCount(limit, startTime, endTime, sort, siteId, busType);
    }

    /**
     * 关锁业务量统计导出
     *
     * @param limit     limit 查询数 【必须】
     * @param startTime startTime 起始时间 yyyy-MM-dd
     * @param endTime   endTime 结束时间 yyyy-MM-dd
     * @param sort      sort 排序规则 desc/asc 【必须】
     * @param siteId    站点id
     * @param busType   查询类型 1：转运业务量 2：抵运业务量 0/其他：全部业务 【必须】
     * @return
     */
    @GetMapping("/route/lockbusinessexport")
    @Timed
    public void lockBusinessExport(HttpServletRequest request, HttpServletResponse response, Integer limit, @RequestParam(value = "startTime", required = false) ZonedDateTime startTime,
                                      @RequestParam(value = "endTime", required = false) ZonedDateTime endTime, String sort,
                                      String siteId, Integer busType) {

        String site_name = "";
        String all_business = "";
        Map<String, Object> currentMp = new HashMap<String, Object>() {
        };
        List<Language> l = languageRepository.findByDefault();
        Language first = null;
        if (l.size() > 0) {
            first = l.get(0);
        }

        if (first != null) {
            if (first.getName().equals("zh")) {
                currentMp = languagesproperties.getZh();
            } else if (first.getName().equals("en")) {
                currentMp = languagesproperties.getEn();

            } else if (first.getName().equals("pu")) {
                currentMp = languagesproperties.getPu();
            }

            if (currentMp != null) {
                site_name = currentMp.get("site_name").toString();
                all_business = currentMp.get("all_business").toString();

            }
        }
        String[] headers = {site_name, all_business};
        List<LockBusinessCount> list = statisticsService.lockBusinessCountExport(limit, startTime, endTime, sort, siteId, busType);
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        //设置列宽
        sheet.setDefaultColumnWidth((short) 18);
        //创建第一行的对象，第一行一般用于填充标题内容。从第二行开始一般是数据
        HSSFRow row = sheet.createRow(0);
        for (
                short i = 0;
                i < headers.length; i++) {
            //创建单元格，每行多少数据就创建多少个单元格
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            //给单元格设置内容
            cell.setCellValue(text);
        }

        //遍历集合，将每个集合元素对象的每个值填充到单元格中
        for (
                int i = 0; i < list.size(); i++) {
            LockBusinessCount item = list.get(i);
            //从第二行开始填充数据
            row = sheet.createRow(i + 1);

            HSSFCell cell = row.createCell(0);
            cell.setCellValue(item.getSiteName());
            HSSFCell cell1 = row.createCell(1);
            cell1.setCellValue(item.getBus());

        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + "Devices.xls");//Excel文件名
        try {
            response.flushBuffer();
        } catch (
                IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //将workbook中的内容写入输出流中
        try {
            workbook.write(response.getOutputStream());
        } catch (
                IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 关锁分布统计
     *
     * @param limit  limit 查询数 【必须】
     * @param sort   sort 排序规则 desc/asc 【必须】
     * @param siteId 站点id
     * @return
     */
    @GetMapping("/route/lockdistributioncount")
    @Timed
    public BaseDTO lockDistributionCount(Integer limit, String sort,
                                         @RequestParam(value = "siteId", required = false) String siteId) {
        return statisticsService.lockDistributionCount(limit, sort, siteId);
    }

    /**
     * 关锁分布统计导出
     *
     * @param limit  limit 查询数 【必须】
     * @param sort   sort 排序规则 desc/asc 【必须】
     * @param siteId 站点id
     * @return
     */
    @GetMapping("/route/lockdistributionexport")
    @Timed
    public void lockDistributionExport(HttpServletRequest request, HttpServletResponse response,Integer limit, String sort,
                                          @RequestParam(value = "siteId", required = false) String siteId) {
        String site_name = "";
        String lock_inventory_count = "";
        Map<String, Object> currentMp = new HashMap<String, Object>() {
        };
        List<Language> l = languageRepository.findByDefault();
        Language first = null;
        if (l.size() > 0) {
            first = l.get(0);
        }

        if (first != null) {
            if (first.getName().equals("zh")) {
                currentMp = languagesproperties.getZh();
            } else if (first.getName().equals("en")) {
                currentMp = languagesproperties.getEn();

            } else if (first.getName().equals("pu")) {
                currentMp = languagesproperties.getPu();
            }

            if (currentMp != null) {
                site_name = currentMp.get("site_name").toString();
                lock_inventory_count = currentMp.get("lock_inventory_count").toString();

            }
        }
        String[] headers = {site_name, lock_inventory_count};
        List<LockBusinessCount> list = statisticsService.lockDistributionCountExport(limit, sort, siteId);
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        //设置列宽
        sheet.setDefaultColumnWidth((short) 18);
        //创建第一行的对象，第一行一般用于填充标题内容。从第二行开始一般是数据
        HSSFRow row = sheet.createRow(0);
        for (
                short i = 0;
                i < headers.length; i++) {
            //创建单元格，每行多少数据就创建多少个单元格
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            //给单元格设置内容
            cell.setCellValue(text);
        }

        //遍历集合，将每个集合元素对象的每个值填充到单元格中
        for (
                int i = 0; i < list.size(); i++) {
            LockBusinessCount item = list.get(i);
            //从第二行开始填充数据
            row = sheet.createRow(i + 1);

            HSSFCell cell = row.createCell(0);
            cell.setCellValue(item.getSiteName());
            HSSFCell cell1 = row.createCell(1);
            cell1.setCellValue(item.getBus());

        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + "Devices.xls");//Excel文件名
        try {
            response.flushBuffer();
        } catch (
                IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //将workbook中的内容写入输出流中
        try {
            workbook.write(response.getOutputStream());
        } catch (
                IOException e) {
            e.printStackTrace();
        }
    }
}
